package com.winit.openapi.intelligent.controller.validator;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.outbound.vo.OutboundOrderEnumContants;
import com.winit.oms.spi.outbound.vo.OutboundOrderEnumContants.OrderType;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.utils.ValidateUtils;

public class IntelligentRecommendfieldValidator {

    public static void validIntelligentRecommend(JSONObject json) {
        validBaseField(json, OrderType.SD.getType());
        validSecondStepBaseField(json);
        validThirdStepBaseField(json);
        validFouthStepBaseField(json);
    }

    public static void validCreateOutboundOrder(JSONObject json) {
        String winitProductCode = json.getString("winitProductCode");
        String orderType = getOrderType(winitProductCode);
        validCreateBaseField(json, orderType);
        validCreateSecondStepBaseField(json);
        validCreateThirdStepBaseField(json);
        validCreateFouthStepBaseField(json);
    }

    /**
     * 通过产品编码获得订单类型
     * 
     * @param winitProductCode 产品编码
     * @return
     */
    public static String getOrderType(String winitProductCode) {
        String orderType = null;
        if (winitProductCode.startsWith(OrderType.SD.getType())) {
            orderType = OrderType.SD.getType();
        } else if (winitProductCode.startsWith(OrderType.SP.getType())) {
            orderType = OrderType.SP.getType();
        } else if (winitProductCode.startsWith(OrderType.DS.getType())) {
            orderType = OrderType.DS.getType();
        }
        return orderType;
    }

    /**
     * 构建共用参数
     * 
     * @param json
     */
    public static void validBaseField(JSONObject json, String orderType) {

        ValidateUtil.validMaxLength(json, "dispatchWhCode", 40, true);
        ValidateUtil.validMaxLength(json, "dispatchWhName", 60, false);
        ValidateUtil.validMaxLength(json, "orderNo", 64, false);

        // 标准出库验证
        if (OrderType.SD.getType().equals(orderType)) {
            // 收件人邮编
            ValidateUtil.validMaxLength(json, "buyerPostcode", 50, true);
            // 收件人国家
            ValidateUtil.validMaxLength(json, "buyerCountry", 50, true);
            // 城市
            ValidateUtil.validMaxLength(json, "buyerCity", 50, true);
            // 州、省
            // ValidateUtil.validMaxLength(json, "buyerState", 50, true);
            // 卖家订单号未校验
            ValidateUtil.validMaxLength(json, "customerOrderNo", 40, false);
            // 是否是ebay订单校验不能为空
            ValidateUtil.validMaxLength(json, "isEbay", 10, true);
            // 是否是重复订单
            ValidateUtil.validMaxLength(json, "isRepeat", 10, false);
            // 是否重复订单
            ValidateUtil.validByRegex(json, "isRepeat", "[YN]");
            // 是否是ebay订单
            ValidateUtil.validByRegex(json, "isEbay", "[YN]");

            if ("Y".equals(json.getString("isEbay"))) {
                ValidateUtil.validMaxLength(json, "ebaySellerId", 30, false);
                ValidateUtil.validMaxLength(json, "ebaybuyerId", 30, false);
            }
        } else if (OrderType.SP.getType().equals(orderType)) {
            // 收货人姓名
            ValidateUtil.validMaxLength(json, "buyerName", 60, true);
            // 收货人电话
            ValidateUtil.validMaxLength(json, "buyerPhone", 20, true);
            // 收货人开始时间
            ValidateUtil.validMaxLength(json, "takeStartTime", 60, true);
            // 结束时间
            ValidateUtil.validMaxLength(json, "takeEndTime", 60, true);
            // 收货人开始时间和结束时间
            ValidateUtil.validDateBefore(json, "takeStartTime", "takeEndTime");
        } else if (OrderType.DS.getType().equals(orderType)) {

        } else {
            // 抛异常：{不支持的订单类型}
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, orderType);
        }
    }

    /**
     * 构建共用参数
     * 
     * @param json
     */
    public static void validCreateBaseField(JSONObject json, String orderType) {

        ValidateUtil.validMaxLength(json, "warehouseCode", 40, true);
        ValidateUtil.validMaxLength(json, "orderNo", 64, false);

        // 标准出库验证
        if (OrderType.SD.getType().equals(orderType)) {
            // 收件人邮编
            ValidateUtil.validMaxLength(json, "receiverPostcode", 50, true);
            // 收件人国家
            ValidateUtil.validMaxLength(json, "receiverCountry", 50, true);
            // 城市
            ValidateUtil.validMaxLength(json, "receiverCity", 50, true);
            // 州、省
            ValidateUtil.validMaxLength(json, "receiverState", 50, false);
            // 卖家订单号未校验
            ValidateUtil.validMaxLength(json, "shipperOrderNo", 40, false);

            // 是否是ebay订单长度校验
            ValidateUtil.validMaxLength(json, "isEbay", 1, false);
            // 是否是ebay订单传值只能是Y/N
            ValidateUtil.validByRegex(json, "isEbay", "[YN]");
            // 是否是重复订不能为空
            ValidateUtil.validMaxLength(json, "isRepeat", 1, true);
            // 是否重复订单传值只能是Y/N
            ValidateUtil.validByRegex(json, "isRepeat", "[YN]");

            // 老系统支持50
            ValidateUtil.validMaxLength(json, "eBaySellerId", 50, false);
            ValidateUtil.validMaxLength(json, "eBayBuyerId", 50, false);
        } else if (OrderType.SP.getType().equals(orderType)) {
            // 收货人姓名
            ValidateUtil.validMaxLength(json, "receiverName", 60, true);
            // 收货人电话
            ValidateUtil.validMaxLength(json, "receiverPhone", 20, true);
            // 收货人开始时间
            ValidateUtil.validMaxLength(json, "pickupDatetimeStart", 60, true);
            // 结束时间
            ValidateUtil.validMaxLength(json, "pickupDatetimeEnd", 60, true);
            // 收货人开始时间和结束时间
            ValidateUtil.validDateBefore(json, "pickupDatetimeStart", "pickupDatetimeEnd");
        } else if (OrderType.DS.getType().equals(orderType)) {

        } else {
            // 抛异常：{不支持的订单类型}
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, orderType);
        }
    }

    /**
     * 校验创建海外仓出库单参数 第一步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveFirstStep
     * 
     * @param json
     */
    public static void validFirstStepField(JSONObject json) {
        ValidateUtil.validNotNull(json, "orderType");
        String orderType = json.getString("orderType");
        validBaseField(json, orderType);
    }

    /**
     * 校验创建海外仓出库单参数 第二步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveSecondStep
     * 
     * @param json
     */
    public static void validSecondStepField(JSONObject json) {
        ValidateUtil.isNotNull(json, "orderNo");
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);
        validSecondStepBaseField(json);
    }

    /**
     * 校验创建海外仓出库单参数 第二步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveSecondStep
     * 
     * @param json
     */
    public static void validSecondStepBaseField(JSONObject json) {
        JSONArray array = json.getJSONArray("merchandiseInfoList");

        if (null == array || array.size() <= 0) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "merchandiseInfoList");
        }
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            ValidateUtil.validMaxLength(obj, "merchandiseCode", 64, true);
            ValidateUtil.validMaxLength(obj, "specification", 64, false);
            ValidateUtil.validMaxLength(obj, "qty", 16, true);
            ValidateUtils.isNumber(obj.getString("qty"), "qty"); // 校验必须是数字
            if ("Y".equals(json.getString("isEbay"))) {
                ValidateUtil.validMaxLength(obj, "ebayTransactionId", 64, true);
                ValidateUtil.validMaxLength(obj, "ebayItemId", 64, true);
            }
        }
    }

    /**
     * 校验创建海外仓出库单参数 第二步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveSecondStep
     * 
     * @param json
     */
    public static void validCreateSecondStepBaseField(JSONObject json) {
        JSONArray array = json.getJSONArray("merchandiseList");

        if (null == array || array.size() <= 0) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "merchandiseList");
        }
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            ValidateUtil.validMaxLength(obj, "merchandiseCode", 64, true);
            ValidateUtil.validMaxLength(obj, "specification", 64, false);
            ValidateUtil.validMaxLength(obj, "quantity", 16, true);
            ValidateUtil.validInteger(obj, "quantity", true);// 需要校验必须为数字
            if (StringUtils.isNotEmpty(obj.getString("eBayTransactionId"))
                || StringUtils.isNotEmpty(obj.getString("eBayItemId"))) {
                ValidateUtil.validMaxLength(obj, "eBayTransactionId", 64, true);
                ValidateUtil.validMaxLength(obj, "eBayItemId", 64, true);
            }
        }
    }

    /**
     * 校验创建海外仓出库单参数 第三步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveThirdStep
     * 
     * @param json
     */
    public static void validThirdStepField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);
        validThirdStepBaseField(json);

    }

    public static void validThirdStepBaseField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "winitProductCode", 32, true);
        ValidateUtil.validMaxLength(json, "invoiceUrl", 256, false);
        if (json.containsKey("takeStartTime")) {// 取件开始时间
            ValidateUtil.validMaxLength(json, "takeStartTime", 60, true);
        }
        if (json.containsKey("takeEndTime")) {// 取件结束时间
            ValidateUtil.validMaxLength(json, "takeEndTime", 60, true);
        }
        if (json.containsKey("takeStartTime") && json.containsKey("takeEndTime")) {
            ValidateUtil.validDateBefore(json, "takeStartTime", "takeEndTime");
        }
    }

    public static void validCreateThirdStepBaseField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "winitProductCode", 32, true);
        ValidateUtil.validMaxLength(json, "invoiceUrl", 256, false);
        if (json.containsKey("pickupDatetimeStart")) {// 取件开始时间
            ValidateUtil.validMaxLength(json, "pickupDatetimeStart", 60, true);
        }
        if (json.containsKey("pickupDatetimeEnd")) {// 取件结束时间
            ValidateUtil.validMaxLength(json, "pickupDatetimeEnd", 60, true);
        }
        if (json.containsKey("pickupDatetimeStart") && json.containsKey("pickupDatetimeEnd")) {
            ValidateUtil.validDateBefore(json, "pickupDatetimeStart", "pickupDatetimeEnd");
        }
    }

    /**
     * 校验创建海外仓出库单参数 第四步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveFouthStep
     * 
     * @param json
     */
    public static void validFouthStepBaseField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "buyerName", 60, true);
        ValidateUtil.validMaxLength(json, "buyerHouseNo", 60, false);
        ValidateUtil.validMaxLength(json, "buyerAddress1", 100, true);
        ValidateUtil.validMaxLength(json, "buyerAddress2", 100, false);
        ValidateUtil.validMaxLength(json, "buyerPhone", 20, true);
        ValidateUtil.validMaxLength(json, "buyerEmail", 50, false);
    }

    /**
     * 校验创建海外仓出库单参数 第四步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveFouthStep
     * 
     * @param json
     */
    public static void validCreateFouthStepBaseField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "receiverName", 60, true);
        ValidateUtil.validMaxLength(json, "receiverHouseNo", 60, false);
        ValidateUtil.validMaxLength(json, "receiverAddressLine1", 100, true);
        ValidateUtil.validMaxLength(json, "receiverAddressLine2", 100, false);
        ValidateUtil.validMaxLength(json, "receiverPhone", 20, false);
        ValidateUtil.validMaxLength(json, "receiverEmail", 50, false);
    }

    /**
     * 校验创建海外仓出库单参数 第四步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveFouthStep
     * 
     * @param json
     */
    public static void validFouthStepField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);
        validFouthStepBaseField(json);
    }

    /**
     * 校验单步提交订单入参
     * 
     * @param json
     */
    public static void validSubmitField(JSONObject json) {
        ValidateUtil.isNotNull("orderNo");
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);

        ValidateUtil.isNotNull(json, "orderType");
        String orderType = json.getString("orderType");
        boolean type = OutboundOrderEnumContants.OrderType.DS.getType().equals(orderType)
                       || OutboundOrderEnumContants.OrderType.SP.getType().equals(orderType);
        if (type) {
            ValidateUtil.isNotNull(json, "winitProductCode");
            ValidateUtil.validMaxLength(json, "winitProductCode", 32, true);
        } else if (OutboundOrderEnumContants.OrderType.SD.getType().equals(orderType)) {
            ValidateUtil.validMaxLength(json, "buyerName", 60, true);
            ValidateUtil.validMaxLength(json, "buyerHouseNo", 60, false);
            ValidateUtil.validMaxLength(json, "buyerAddress1", 100, true);
            ValidateUtil.validMaxLength(json, "buyerAddress2", 100, false);
            ValidateUtil.validMaxLength(json, "buyerPhone", 20, false);
            ValidateUtil.validMaxLength(json, "buyerEmail", 50, false);
        }
    }

    /**
     * 出库单列表查询校验.
     * 
     * @param json
     */
    public static void validQueryByMainOrderParam(JSONObject json) {

        if (json.containsKey("orderNo")) {// 订单号
            ValidateUtil.validMaxLength(json, "orderNo", 64);
        }
        if (json.containsKey("winitTrackingNo")) {// winit跟踪号
            ValidateUtil.validMaxLength(json, "winitTrackingNo", 64);
        }
        if (json.containsKey("sellerOrderNo")) {// 卖家订单号
            ValidateUtil.validMaxLength(json, "sellerOrderNo", 40);
        }
        if (json.containsKey("orderStatus")) {// 订单状态
            ValidateUtil.validMaxLength(json, "orderStatus", 30);
        }
        if (json.containsKey("buyerName")) {// 收件人姓名
            ValidateUtil.validMaxLength(json, "buyerName", 60);
        }
        if (json.containsKey("dispatchWhCode")) {// 发货仓编号
            ValidateUtil.validMaxLength(json, "dispatchWhCode", 40);
        }
        if (json.containsKey("orderType")) {// 订单类型
            ValidateUtil.validMaxLength(json, "orderType", 30);
        }
        if (json.containsKey("trackingNo")) {// 快递单号
            ValidateUtil.validMaxLength(json, "trackingNo", 32);
        }
        if (json.containsKey("merchandiseCode")) {// 商品编码
            ValidateUtil.validMaxLength(json, "merchandiseCode", 255);
        }
        if (json.containsKey("merchandiseName")) {// 商品名称
            ValidateUtil.validMaxLength(json, "merchandiseName", 255);
        }
        if (json.containsKey("orderSource")) {// 订单来源
            ValidateUtil.validMaxLength(json, "orderSource", 10);
        }
        if (json.containsKey("customerName")) {// 下单人
            ValidateUtil.validMaxLength(json, "customerName", 60);
        }
    }

    /**
     * 校验出库单修改信息.
     * 
     * @param json
     */
    public static void validateModifyOutboundOrder(JSONObject json) {
        JSONObject obj = json.getJSONObject("buyerInfoVo");
        ValidateUtil.validNotNull(json, "orderNo");
        ValidateUtil.validNotNull(obj, "buyerName");
        ValidateUtil.validNotNull(obj, "buyerPhone");
        ValidateUtil.validNotNull(obj, "buyerCountry");
        ValidateUtil.validNotNull(obj, "buyerCity");
        ValidateUtil.validNotNull(obj, "buyerPostcode");
        ValidateUtil.validNotNull(obj, "buyerAddress1");
        ValidateUtil.validMaxLength(obj, "orderNo", 64);
        ValidateUtil.validMaxLength(obj, "buyerName", 60);
        ValidateUtil.validMaxLength(obj, "buyerEmail", 50);
        ValidateUtil.validMaxLength(obj, "buyerPhone", 20);
        ValidateUtil.validMaxLength(obj, "buyerHouseNo", 60);
        ValidateUtil.validMaxLength(obj, "buyerCountry", 50);
        ValidateUtil.validMaxLength(obj, "buyerState", 50);
        ValidateUtil.validMaxLength(obj, "buyerCity", 50);
        ValidateUtil.validMaxLength(obj, "buyerPostcode", 50);
        ValidateUtil.validMaxLength(obj, "buyerAddress1", 100);
        ValidateUtil.validMaxLength(obj, "buyerAddress2", 100);
    }

    /**
     * 校验订单入参.
     * 
     * @param json
     */
    public static void validreOrderField(JSONObject json) {
        if (json.containsKey("orderNo")) {// 订单号
            ValidateUtil.validNotNull(json, "orderNo");
            ValidateUtil.validMaxLength(json, "orderNo", 64);
        }
    }

    /**
     * 校验计价参数信息.
     * 
     * @param json
     */
    public static void validParam(JSONObject json) {
        if (json.containsKey("warehouseCode")) {
            ValidateUtil.validNotNull(json, "warehouseCode");
            ValidateUtil.validMaxLength(json, "warehouseCode", 128);
        }
        if (json.containsKey("countryCode")) {
            ValidateUtil.validNotNull(json, "countryCode");
            ValidateUtil.validMaxLength(json, "countryCode", 128);
        }
        if (json.containsKey("destinationPostcode")) {
            ValidateUtil.validNotNull(json, "destinationPostcode");
            ValidateUtil.validMaxLength(json, "destinationPostcode", 16);
        }
        if (json.containsKey("length")) {
            ValidateUtil.validBigDecimal(json, "length", true);
            ValidateUtil.validatePrecision(json, "length", 16, 2);
        }
        if (json.containsKey("width")) {
            ValidateUtil.validBigDecimal(json, "width", true);
            ValidateUtil.validatePrecision(json, "width", 16, 2);
        }
        ValidateUtil.validBigDecimal(json, "height", true);
        if (json.containsKey("height")) {
            ValidateUtil.validatePrecision(json, "height", 16, 2);
        }
        if (json.containsKey("weight")) {
            ValidateUtil.validBigDecimal(json, "weight", true);
            ValidateUtil.validatePrecision(json, "weight", 16, 4);
        }
    }

    /**
     * 校验返利传入参数信息.
     * 
     * @param json
     */

    public static void validRebate(JSONObject json) {
        if (json.containsKey("orderNo")) {// 订单号
            ValidateUtil.validMaxLength(json, "orderNo", 60);
        }
        if (json.containsKey("orderType")) {// 订单号
            ValidateUtil.validMaxLength(json, "orderType", 8);
        }
        if (json.containsKey("sellerOrderNo")) {// 卖家订单号
            ValidateUtil.validMaxLength(json, "sellerOrderNo", 40);
        }
        if (json.containsKey("warehouseCode")) {// 倉庫名稱
            ValidateUtil.validMaxLength(json, "warehouseCode", 40);
        }
        if (json.containsKey("buyerName")) {// 收件人姓名
            ValidateUtil.validMaxLength(json, "buyerName", 60);
        }
        if (json.containsKey("orderDateFrom")) {
            ValidateUtil.validMaxLength(json, "orderDateFrom", 60);
        }
        if (json.containsKey("orderDateFromTo")) {
            ValidateUtil.validMaxLength(json, "orderDateFromTo ", 60);
        }
        if (json.containsKey("rebateStatus")) {// 返利状态
            ValidateUtil.validMaxLength(json, "rebateStatus", 60);
        }
    }

    /**
     * 校验返利导入传入参数信息.
     * 
     * @param json
     */

    public static void validImportRebate(JSONObject json) {
        if (json.containsKey("warehouseName")) {// 仓库名称
            ValidateUtil.validMaxLength(json, "warehouseName", 60);
        }
        if (json.containsKey("sellerId")) {// 卖家id
            ValidateUtil.validMaxLength(json, "sellerId", 32);
        }
        if (json.containsKey("ebayTransactionId")) {// eBay交易ID
            ValidateUtil.validMaxLength(json, "ebayTransactionId", 64);
        }
        if (json.containsKey("ebayItemId")) {// eBay商品ID
            ValidateUtil.validMaxLength(json, "ebayItemId", 64);
        }
        if (json.containsKey("buyerId")) {// 买家id
            ValidateUtil.validMaxLength(json, "buyerId", 32);
        }
        if (json.containsKey("sellerOrderNo")) { // 卖家订单号
            ValidateUtil.validMaxLength(json, "sellerOrderNo", 60);
        }
        if (json.containsKey("orderNo")) { // 出库单编号
            ValidateUtil.validMaxLength(json, "orderNo ", 60);
        }
        if (json.containsKey("rebateStatus")) {// 返利状态
            ValidateUtil.validMaxLength(json, "rebateStatus", 60);
        }
    }
}
